home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / GadTools / gadget1.c next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  9.3 KB  |  390 lines

  1. /*
  2. ** gadget1.c:  Simple example of GadTools gadgets.
  3. **
  4. ** (C) Copyright 1990, Commodore-Amiga, Inc.
  5. **
  6. ** Executables based on this information may be used in software
  7. ** for Commodore Amiga computers.  All other rights reserved.
  8. ** This information is provided "as is"; no warranties are made.  All
  9. ** use is at your own risk. No liability or responsibility is assumed.
  10. */
  11.  
  12. #include <exec/types.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/gadgetclass.h>
  15. #include <libraries/gadtools.h>
  16.  
  17. #include <clib/exec_protos.h>
  18. #include <clib/graphics_protos.h>
  19. #include <clib/intuition_protos.h>
  20. #include <clib/gadtools_protos.h>
  21.  
  22. void printf(STRPTR,...);
  23. void exit(int);
  24.  
  25. /*------------------------------------------------------------------------*/
  26.  
  27. void main(void);
  28. void bail_out(int, STRPTR);
  29. BOOL HandleGadgetEvent(struct Window *, struct Gadget *, UWORD);
  30. struct Gadget *CreateAllGadgets(struct Gadget **, void *, UWORD);
  31.  
  32. /*------------------------------------------------------------------------*/
  33.  
  34. /* Gadget defines of our choosing, to be used as GadgetID's: */
  35.  
  36. #define GAD_SLIDER    1
  37. #define GAD_STRING    2
  38. #define GAD_BUTTON    3
  39.  
  40. /* Range for the slider: */
  41.  
  42. #define SLIDER_MIN    1
  43. #define SLIDER_MAX    20
  44.  
  45. /*------------------------------------------------------------------------*/
  46.  
  47. struct TextAttr Topaz80 =
  48. {
  49.     "topaz.font",    /* Name */
  50.     8,            /* YSize */
  51.     0,            /* Style */
  52.     0,            /* Flags */
  53. };
  54.  
  55. extern struct Library *SysBase;
  56. struct GfxBase *GfxBase = NULL;
  57. struct IntuitionBase *IntuitionBase = NULL;
  58. struct Library *GadToolsBase = NULL;
  59. struct TextFont *font = NULL;
  60. struct Screen *mysc = NULL;
  61. struct Gadget *glist = NULL;
  62. struct Window *mywin = NULL;
  63. void *vi = NULL;
  64.  
  65. BOOL terminated = FALSE;
  66. WORD slider_level = 5;
  67. struct Gadget *slidergad;
  68.  
  69. void main(void)
  70. {
  71.     struct IntuiMessage *imsg;
  72.     struct Gadget *gad;
  73.     ULONG imsgClass;
  74.     UWORD imsgCode;
  75.     UWORD topborder;
  76.  
  77.     /* Open all libraries: */
  78.  
  79.     if (!(GfxBase = (struct GfxBase *)
  80.     OpenLibrary("graphics.library", 36L)))
  81.     bail_out(20, "Requires V36 graphics.library");
  82.  
  83.     if (!(IntuitionBase = (struct IntuitionBase *)
  84.     OpenLibrary("intuition.library", 36L)))
  85.     bail_out(20, "Requires V36 intuition.library");
  86.  
  87.     if (!(GadToolsBase = OpenLibrary("gadtools.library", 36L)))
  88.     bail_out(20, "Requires V36 gadtools.library");
  89.  
  90.     /* Open topaz 8 font, so we can be sure it's openable
  91.      * when we later set ng_TextAttr to &Topaz80:
  92.      */
  93.     if (!(font = OpenFont(&Topaz80)))
  94.     bail_out(20, "Failed to open Topaz 80");
  95.  
  96.     if (!(mysc = LockPubScreen(NULL)))
  97.     bail_out(20, "Couldn't lock default public screen");
  98.  
  99.     if (!(vi = GetVisualInfo(mysc,
  100.     TAG_DONE)))
  101.     bail_out(20, "GetVisualInfo() failed");
  102.  
  103.     /* Here is how we can figure out ahead of time how tall the
  104.      * window's title bar will be:
  105.      */
  106.     topborder = mysc->WBorTop + (mysc->Font->ta_YSize + 1);
  107.  
  108.     if (!CreateAllGadgets(&glist, vi, topborder))
  109.     {
  110.     bail_out(20, "CreateAllGadgets() failed");
  111.     }
  112.  
  113.     if (!(mywin = OpenWindowTags(NULL,
  114.     WA_Width, 400,
  115.     WA_InnerHeight, 100,
  116.  
  117.     WA_Activate, TRUE,
  118.     WA_DragBar, TRUE,
  119.     WA_DepthGadget, TRUE,
  120.     WA_CloseGadget, TRUE,
  121.     WA_SizeGadget, TRUE,
  122.     WA_SimpleRefresh, TRUE,
  123.  
  124.     WA_IDCMP, CLOSEWINDOW | REFRESHWINDOW | \
  125.         SLIDERIDCMP | STRINGIDCMP | BUTTONIDCMP,
  126.  
  127.     WA_MinWidth, 50,
  128.     WA_MinHeight, 50,
  129.     WA_Title, "GadTools Gadget Demo 1",
  130.  
  131.     /* Gadgets go here, or in NewWindow.FirstGadget */
  132.     WA_Gadgets, glist,
  133.  
  134.     TAG_DONE)))
  135.     bail_out(20, "OpenWindow() failed");
  136.  
  137.     /* After window is open, we must call this GadTools refresh
  138.      * function.
  139.      */
  140.     GT_RefreshWindow(mywin, NULL);
  141.  
  142.     while (!terminated)
  143.     {
  144.     Wait (1 << mywin->UserPort->mp_SigBit);
  145.     /* GT_GetIMsg() returns a cooked-up IntuiMessage with
  146.      * more friendly information for complex gadget classes.  Use
  147.      * it wherever you get IntuiMessages:
  148.      */
  149.     while ((!terminated) && (imsg = GT_GetIMsg(mywin->UserPort)))
  150.     {
  151.         imsgClass = imsg->Class;
  152.         imsgCode = imsg->Code;
  153.         /* Presuming a gadget, of course, but no harm... */
  154.         gad = (struct Gadget *)imsg->IAddress;
  155.         /* Use the toolkit message-replying function here... */
  156.         GT_ReplyIMsg(imsg);
  157.         switch (imsgClass)
  158.         {
  159.         case GADGETUP:
  160.             printf("GADGETUP.  ");
  161.             terminated = HandleGadgetEvent(mywin, gad, imsgCode);
  162.             break;
  163.  
  164.         case GADGETDOWN:
  165.             printf("GADGETDOWN.  ");
  166.             terminated = HandleGadgetEvent(mywin, gad, imsgCode);
  167.             break;
  168.  
  169.         case MOUSEMOVE:
  170.             printf("MOUSEMOVE.  ");
  171.             terminated = HandleGadgetEvent(mywin, gad, imsgCode);
  172.             break;
  173.  
  174.         case CLOSEWINDOW:
  175.             printf("CLOSEWINDOW.\n");
  176.             terminated = TRUE;
  177.             break;
  178.  
  179.         case REFRESHWINDOW:
  180.             printf("REFRESHWINDOW.\n");
  181.             /* You must use GT_BeginRefresh() where you would
  182.              * normally have your first BeginRefresh()
  183.              */
  184.             GT_BeginRefresh(mywin);
  185.             GT_EndRefresh(mywin, TRUE);
  186.             break;
  187.         }
  188.     }
  189.     }
  190.     bail_out(0, NULL);
  191. }
  192.  
  193. /*------------------------------------------------------------------------*/
  194.  
  195. /*/ bail_out()
  196.  *
  197.  * Function to close down or free any opened or allocated stuff, and then
  198.  * exit.
  199.  *
  200.  */
  201.  
  202. void bail_out(code, error)
  203.  
  204. int code;
  205. STRPTR error;
  206.  
  207. {
  208.     if (mywin)
  209.     {
  210.     CloseWindow(mywin);
  211.     }
  212.  
  213.     /* None of these two calls mind a NULL parameter, so it's not
  214.      * necessary to check for non-NULL before calling.  If we do that,
  215.      * we must be certain that the OpenLibrary() of GadTools succeeded,
  216.      * or else we would be jumping into outer space:
  217.      */
  218.     if (GadToolsBase)
  219.     {
  220.     FreeVisualInfo(vi);
  221.     FreeGadgets(glist);
  222.     CloseLibrary(GadToolsBase);
  223.     }
  224.  
  225.     if (mysc)
  226.     {
  227.     UnlockPubScreen(NULL, mysc);
  228.     }
  229.  
  230.     if (font)
  231.     {
  232.         CloseFont(font);
  233.     }
  234.  
  235.     if (IntuitionBase)
  236.     {
  237.     CloseLibrary(IntuitionBase);
  238.     }
  239.  
  240.     if (GfxBase)
  241.     {
  242.     CloseLibrary(GfxBase);
  243.     }
  244.  
  245.     if (error)
  246.     {
  247.     printf("Error: %s\n", error);
  248.     }
  249.     exit(code);
  250. }
  251.  
  252.  
  253. /*------------------------------------------------------------------------*/
  254.  
  255. /*/ HandleGadgetEvent()
  256.  *
  257.  * Function to handle a GADGETUP or GADGETDOWN event.  For toolkit gadgets,
  258.  * it is possible to use this function to handle MOUSEMOVEs as well, with
  259.  * little or no work.
  260.  *
  261.  */
  262.  
  263. BOOL HandleGadgetEvent(win, gad, code)
  264.  
  265. struct Window *win;
  266. struct Gadget *gad;
  267. UWORD code;
  268.  
  269. {
  270.     BOOL terminated = FALSE;
  271.  
  272.     switch (gad->GadgetID)
  273.     {
  274.     case GAD_SLIDER:
  275.         /* Sliders report their level in the IntuiMessage Code
  276.          * field:
  277.          */
  278.         printf("Slider at level %ld\n", code);
  279.         slider_level = code;
  280.         break;
  281.  
  282.     case GAD_STRING:
  283.         /* String gadgets report GADGETUP's */
  284.         printf("String gadget: '%s'.\n",
  285.         ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  286.         break;
  287.  
  288.     case GAD_BUTTON:
  289.         /* Buttons always report GADGETUP's, nothing
  290.          * fancy or different here
  291.          */
  292.         printf("Button was pressed.\n");
  293.         /* increment the slider, or wrap it around: */
  294.         if (++slider_level > SLIDER_MAX)
  295.         {
  296.         slider_level = SLIDER_MIN;
  297.         }
  298.         GT_SetGadgetAttrs(slidergad, win, NULL,
  299.         GTSL_Level, slider_level,
  300.         TAG_DONE);
  301.         break;
  302.     }
  303.     return(terminated);
  304. }
  305.  
  306.  
  307. /*------------------------------------------------------------------------*/
  308.  
  309. /*/ CreateAllGadgets()
  310.  *
  311.  * Here is where all the initialization and creation of toolkit gadgets
  312.  * take place.  This function requires a pointer to a NULL-initialized
  313.  * gadget list pointer.  It returns a pointer to the last created gadget,
  314.  * which can be checked for success/failure.
  315.  *
  316.  */
  317.  
  318. struct Gadget *CreateAllGadgets(glistptr, vi, topborder)
  319.  
  320. struct Gadget **glistptr;
  321. void *vi;
  322. UWORD topborder;
  323.  
  324. {
  325.     struct NewGadget ng;
  326.  
  327.     struct Gadget *gad;
  328.  
  329.     /* All the gadget creation calls accept a pointer to the previous
  330.      * gadget, and link the new gadget to that gadget's NextGadget field.
  331.      * Also, they exit gracefully, returning NULL, if any previous gadget
  332.      * was NULL.  This limits the amount of checking for failure that
  333.      * is needed.  You only need to check before you tweak any gadget
  334.      * structure or use any of its fields, and finally once at the end,
  335.      * before you add the gadgets.
  336.      */
  337.  
  338.     /* We obligingly perform the following operation, required of
  339.      * any program that uses the toolkit.  It gives the toolkit a
  340.      * place to stuff context data:
  341.      */
  342.     gad = CreateContext(glistptr);
  343.  
  344.     /* Since the NewGadget structure is unmodified by any of the
  345.      * CreateGadget() calls, we need only change those fields which
  346.      * are different.
  347.      */
  348.  
  349.     ng.ng_LeftEdge = 100;
  350.     ng.ng_TopEdge = 20+topborder;
  351.     ng.ng_Width = 200;
  352.     ng.ng_Height = 12;
  353.     ng.ng_GadgetText = "Speed:   ";
  354.     ng.ng_TextAttr = &Topaz80;
  355.     ng.ng_VisualInfo = vi;
  356.     ng.ng_GadgetID = GAD_SLIDER;
  357.     ng.ng_Flags = NG_HIGHLABEL;
  358.  
  359.     slidergad = gad = CreateGadget(SLIDER_KIND, gad, &ng,
  360.     GTSL_Min, SLIDER_MIN,
  361.     GTSL_Max, SLIDER_MAX,
  362.     GTSL_Level, slider_level,
  363.     GTSL_LevelFormat, "%2ld",
  364.     GTSL_MaxLevelLen, 2,
  365.     TAG_DONE);
  366.  
  367.     ng.ng_TopEdge = 40+topborder;
  368.     ng.ng_Height = 14;
  369.     ng.ng_GadgetText = "Type Here:";
  370.     ng.ng_GadgetID = GAD_STRING;
  371.     gad = CreateGadget(STRING_KIND, gad, &ng,
  372.     GTST_String, "Hello World!",
  373.     GTST_MaxChars, 50,
  374.     TAG_DONE);
  375.  
  376.     ng.ng_LeftEdge += 50;
  377.     ng.ng_TopEdge = 60+topborder;
  378.     ng.ng_Width = 100;
  379.     ng.ng_Height = 12;
  380.     ng.ng_GadgetText = "Click Here";
  381.     ng.ng_GadgetID = GAD_BUTTON;
  382.     ng.ng_Flags = 0;
  383.     gad = CreateGadget(BUTTON_KIND, gad, &ng,
  384.     TAG_DONE);
  385.  
  386.     return(gad);
  387. }
  388.  
  389. /*------------------------------------------------------------------------*/
  390.